import { Body, Controller, Delete, Get, Param, Post, Patch, ParseIntPipe } from '@nestjs/common'
import { ApiOperation, ApiTags, ApiBearerAuth, ApiParam, ApiResponse } from '@nestjs/swagger'
import { definePermission, Perm } from '~/modules/admin/auth/decorators/permission.decorator'
import { AuthUser } from '~/modules/admin/auth/decorators/auth-user.decorator'

import { CreateMainPlanDto } from './dto/create-main-plan.dto'
import { UpdateMainPlanDto } from './dto/update-main-plan.dto'
import { StudentCreateMainPlanDto } from './dto/student-create-main-plan.dto'
import { MainPlanService } from './main-plan.service'

const permissions = definePermission('admin.study.plan.mainPlan', {
  CREATE: 'create',
  COPY: 'copy',
  LIST: 'findAll',
  READ: 'findOne',
  UPDATE: 'update',
  DELETE: 'remove',
  CHECKIN: 'checkin',
  STUDENT_CREATE: 'student_create'
} as const)

@Controller('admin/study/plan/main-plan')
@ApiTags('主计划管理')
@ApiBearerAuth()
export class MainPlanController {
  constructor(private readonly mainPlanService: MainPlanService) {}

  @Post()
  @ApiOperation({ summary: '创建主计划' })
  @Perm(permissions.CREATE)
  create(@Body() createMainPlanDto: CreateMainPlanDto, @AuthUser('uid') userId: number) {
    return this.mainPlanService.create(createMainPlanDto, userId)
  }

  @Get()
  @ApiOperation({ summary: '查询所有主计划' })
  @Perm(permissions.LIST)
  findAll() {
    return this.mainPlanService.findAll()
  }

  @Get(':id')
  @ApiOperation({ summary: '查询单个主计划' })
  @Perm(permissions.READ)
  findOne(@Param('id') id: string) {
    return this.mainPlanService.findOne(+id)
  }

  @Patch(':id')
  @ApiOperation({ summary: '更新主计划' })
  @Perm(permissions.UPDATE)
  update(@Param('id') id: string, @Body() updateMainPlanDto: UpdateMainPlanDto, @AuthUser('uid') userId: number) {
    return this.mainPlanService.update(+id, updateMainPlanDto, userId)
  }

  @Delete(':id')
  @ApiOperation({ summary: '删除主计划' })
  @Perm(permissions.DELETE)
  remove(@Param('id') id: string) {
    return this.mainPlanService.remove(+id)
  }

  @Post(':id/checkin/:studentId')
  @ApiOperation({ summary: '学生打卡学习计划', description: '记录学生对特定学习计划的打卡，用于计算学习进度。每个学生每天在同一学习计划只能打卡一次。' })
  @ApiParam({ name: 'id', description: '主计划ID' })
  @ApiParam({ name: 'studentId', description: '学生ID' })
  @ApiResponse({ status: 200, description: '打卡成功', schema: {
    type: 'object',
    properties: {
      message: { type: 'string', example: '打卡成功' },
      checkinDays: { type: 'number', example: 12, description: '累计打卡天数' },
      mainPlanProgress: { type: 'number', example: 65, description: '主计划完成进度' }
    }
  } })
  @Perm(permissions.CHECKIN)
  checkinMainPlan(
    @Param('id', ParseIntPipe) id: number,
    @Param('studentId', ParseIntPipe) studentId: number,
    @AuthUser('uid') userId: number
  ) {
    return this.mainPlanService.checkinMainPlan(id, studentId, userId);
  }

  @Post('student-create')
  @ApiOperation({ summary: '学生创建主计划', description: '学生创建自己的学习计划，会自动关联到学生所在的班级' })
  @ApiResponse({ status: 201, description: '创建成功' })
  @Perm(permissions.STUDENT_CREATE)
  studentCreateMainPlan(@Body() studentCreateMainPlanDto: StudentCreateMainPlanDto, @AuthUser('uid') userId: number) {
    return this.mainPlanService.studentCreateMainPlan(studentCreateMainPlanDto, userId)
  }
}
